import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/home/index.vue'
import HomeView from '../views/home/HomeView.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    components: {
      'Index': Index,
      'HomeView': HomeView,
    },
    meta: {
      title: '掘车猿',
    }
  },
  {
    // 开启 props 接收参数功能
    props: true,
    path: '/cardetail/:urlId',
    name: 'CarDetails',
    component: () => import('../views/home/CarDetails.vue'),
    meta: {
      title: '车辆详情 -- 掘车猿',
    }
  },
  {
    path: '/personview/:uid',
    name: 'PersonView',
    component: () => import('../views/bbs/PersonView.vue'),
    meta: {
      title: '个人信息 -- 掘车猿',
    }
  },

  {
    // 开启 props 接收参数功能
    props: true,
    // 将 carName 设置为可选参数，可接受空值
    path: '/search/:carName?',
    name: 'SearchView',
    component: () => import('../views/home/SearchView.vue'),
    meta: {
      title: '车辆查询 -- 掘车猿',
    }

  },
  {
    path: '/forum',
    component: () => import('../views/bbs/ForumView.vue'),
    meta: {
      title: '车辆论坛 -- 掘车猿',
    }
  }
  ,
  {
    path: '/postview',
    name: 'PostView',
    component: () => import('../views/bbs/PostView.vue'),
    meta: {
      title: '车友圈 -- 掘车猿',
    }
  },

  {
    path: '/groupview',
    
    component:() => import('../views/bbs/GroupView.vue')
  },

  {
    path:'/login',
    component:() => import('../views/home/LoginView.vue')
  },

  {
    path: '/admin',
    name: 'AdminView',
    component: () => import('../views/admin/AdminView.vue'),
    children: [
      {
        path: 'user/list',
        name: 'UserListView',
        component: () => import('../views/admin/menu/UserListView.vue')
      },
      {
        path: 'user/add-new', // /admin/user/add-new
        name: 'UserAddNewView',
        component: () => import('../views/admin/menu/UserAddNewView.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})

// beforeEach: 路由前置守卫，在路由跳转前触发
router.beforeEach((to, from, next) => {
  // to：跳转链接
  // from：原始链接

  // 修改 title
  document.title = to.meta.title

  // next：放行，允许路由继续运行
  next();
});

export default router
